import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../components/Login";

const Welcome = () => import('../components/Welcome')
const Home = () => import('../components/Home')
const Users = () => import('../components/user/Users')
const Rights = () => import('../components/power/Rights')
const Roles = () => import('../components/power/Roles')
const Cate = () => import('../components/goods/Cate')
const Params = () => import('../components/goods/Params')
const List = () => import('../components/goods/List')
const Report = () => import('../components/report/Report')
const Order = () => import('../components/order/Order')
const Add = () => import('../components/goods/Add')
const Edit = () => import('../components/goods/Edit')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome,
      },
      {
        path: '/users',
        component: Users,
      },
      {
        path: '/roles',
        component: Roles,
      },
      {
        path: '/rights',
        component: Rights,
      },
      {
        path: '/categories',
        component: Cate,
      },
      {
        path: '/goods',
        component: List,
      },
      {
        path: '/params',
        component: Params,
      },
      {
        path: '/orders',
        component: Order,
      },
      {
        path: '/reports',
        component: Report,
      },
      {
        path: '/goods/add',
        component: Add,
      },
      {
        path: '/goods/edit',
        name: 'edit',
        component: Edit,
      },
    ]
  }
]

const router = new VueRouter({
  routes,
  // mode:'history'
})

//挂载路由导航守卫，没有登陆时跳转登录页面，不允许直接访问别的页面
//to将要访问的路径 ，from从哪个路径过来,next()放行,next('/login')强制跳转
router.beforeEach((to, from, next) => {
  //如果用户访问登陆页，直接放行

  if (to.path === '/login') return next()
  //获取token
  const token = window.sessionStorage.getItem('token')
  // console.log(token);
  if (!token) return next('/login')
  next()
})

export default router
